package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guigu.base.entity.TransportLine;
import com.guigu.base.entity.dto.TransportLineDto;
import com.guigu.base.entity.dto.TransportTripsDto;
import com.guigu.base.entity.vo.TransportLineVO;
import com.guigu.base.service.TransportLineService;
import com.guigu.common.utils.PageResponse;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 线路表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/goods/pd-transport-line")
@Api(tags = "线路表")
public class TransportLineController {
    @Autowired
    private TransportLineService transportLineService;

    /**
     * 添加线路
     *
     * @param dto 线路信息
     * @return 线路信息
     */
    @PostMapping("/")
    @ApiOperation("添加线路")
    public R<TransportLineDto> saveTransportLine(@RequestBody TransportLineDto dto) {
        TransportLine TransportLine = new TransportLine();
        BeanUtils.copyProperties(dto, TransportLine);
        TransportLine = transportLineService.saveTransportLine(TransportLine);
        BeanUtils.copyProperties(TransportLine, dto);
        return new R<>("2000", dto);
    }

    /**
     * 根据id获取线路详情
     *
     * @param id 线路id
     * @return 线路详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取线路详情")
    public R<TransportLineDto> fineById(@PathVariable(name = "id") String id) {
        TransportLine transportLine = transportLineService.getById(id);
        if (ObjectUtils.isEmpty(transportLine)) {
            return new R<>("2000", null);
        }
        TransportLineDto dto = new TransportLineDto();
        if (transportLine != null) {
            BeanUtils.copyProperties(transportLine, dto);
        } else {
            dto.setId(id);
        }
        return new R<>("2000", dto);
    }

    /**
     * 获取线路分页信息
     *
     * @param page                页码
     * @param pageSize            页尺寸
     * @param lineNumber          线路编号
     * @param name                线路名称
     * @param transportLineTypeId 线路类型id
     * @return 线路分页信息
     */
    @GetMapping("/page")
    @ApiOperation("获取线路分页信息")
    public R<PageResponse<TransportLineDto>> findByPage(@RequestParam(name = "page") Integer page,
                                                        @RequestParam(name = "pageSize") Integer pageSize,
                                                        @RequestParam(name = "lineNumber", required = false) String lineNumber,
                                                        @RequestParam(name = "name", required = false) String name,
                                                        @RequestParam(name = "transportLineTypeId", required = false) String transportLineTypeId) {
        IPage<TransportLine> transportLinePage = transportLineService.findByPage(page, pageSize, lineNumber, name, transportLineTypeId);
        List<TransportLineDto> dtoList = new ArrayList<>();
        transportLinePage.getRecords().forEach(TransportLine -> {
            TransportLineDto dto = new TransportLineDto();
            BeanUtils.copyProperties(TransportLine, dto);
            dtoList.add(dto);
        });
        return new R<>("2000", PageResponse.<TransportLineDto>builder().items(dtoList).pagesize(pageSize).page(page)
                .counts(transportLinePage.getTotal()).pages(transportLinePage.getPages()).build());
    }

    /**
     * 获取线路列表
     *
     * @param ids 线路id列表
     * @return 线路列表
     */
    @GetMapping("")
    @ApiOperation("获取线路列表")
    public R<List<TransportLineDto>> findAll(@RequestParam(name = "ids", required = false) List<String> ids,
                                             @RequestParam(name = "agencyId", required = false) String agencyId,
                                             @RequestParam(name = "agencyIds", required = false) List<String> agencyIds) {
        return new R<>("2000", transportLineService.findAll(ids, agencyId, agencyIds).stream().map(TransportLine -> {
            TransportLineDto dto = new TransportLineDto();
            BeanUtils.copyProperties(TransportLine, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

    /**
     * 更新线路信息
     *
     * @param id  线路id
     * @param dto 线路信息
     * @return 线路信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新线路信息")
    public R<TransportLineDto> update(@PathVariable(name = "id") String id, @RequestBody TransportLineDto dto) {
        dto.setId(id);
        TransportLine TransportLine = new TransportLine();
        BeanUtils.copyProperties(dto, TransportLine);
        transportLineService.updateById(TransportLine);
        return new R("2000", dto);
    }

    /**
     * 删除线路
     *
     * @param id 线路id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除线路")
    public R disable(@PathVariable(name = "id") String id) {
        transportLineService.disable(id);
        return new R(ResultCode.SUCCESS_CODE, "删除线路成功!", true);
    }


    /**
     * 获取线路列表
     *
     * @return 线路列表
     */
    @PostMapping("list")
    @ApiOperation("获取线路列表")
    public R<List<TransportLineDto>> list(@RequestBody TransportLineDto transportLineDto) {
        LambdaQueryWrapper<TransportLine> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getStartAgencyId()), TransportLine::getStartAgencyId, transportLineDto.getStartAgencyId());
        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getEndAgencyId()), TransportLine::getEndAgencyId, transportLineDto.getEndAgencyId());
        wrapper.eq(StringUtils.isNotEmpty(transportLineDto.getAgencyId()), TransportLine::getAgencyId, transportLineDto.getAgencyId());
        wrapper.eq(null != (transportLineDto.getStatus()), TransportLine::getStatus, transportLineDto.getStatus());

        return new R<>("2000", transportLineService.list(wrapper).stream().map(TransportLine -> {
            TransportLineDto dto = new TransportLineDto();
            BeanUtils.copyProperties(TransportLine, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

    /**
     * 根据起始机构Id和结束机构id来查找对应的线路信息
     *
     * @param startAgencyId 起始机构Id
     * @param endAgencyId   结束机构Id
     * @return
     */
    @ApiOperation("根据起始机构Id和结束机构id来查找对应的线路信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startAgencyId", value = "开始机构Id"),
            @ApiImplicitParam(name = "endAgencyId", value = "终点机构Id")
    })
    @GetMapping("/getTransportLineById/{startAgencyId}/{endAgencyId}")
    public R<TransportLine> getTransportLineById(@PathVariable String startAgencyId,
                                                 @PathVariable String endAgencyId) {
        QueryWrapper<TransportLine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("start_agency_id", startAgencyId);
        queryWrapper.eq("end_agency_id", endAgencyId);
        TransportLine line = transportLineService.getOne(queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, line);
    }

    @ApiOperation("查询该线路下的车次车辆司机信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "线路id"),
            @ApiImplicitParam(name = "truckId", value = "车辆Id"),
            @ApiImplicitParam(name = "driverId", value = "司机Id"),
            @ApiImplicitParam(name = "tripsId", value = "车次Id")
    })
    @GetMapping("/findTransportLineVOById")
    public R<TransportLineVO> findTransportLineVOById(@RequestParam(required = false, value = "id") String id,
                                                      @RequestParam(required = false, value = "truckId") String truckId,
                                                      @RequestParam(required = false, value = "driverId") String driverId,
                                                      @RequestParam(required = false, value = "tripsId") String tripsId) {
        R<TransportLineVO> r = transportLineService.findTransportLineVOById(id, truckId, driverId, tripsId);
        return r;
    }

    /**
     * 查询全部线路信息
     *
     * @return
     */
    @ApiOperation("查询全部线路信息")
    @GetMapping("/queryAll")
    public R<List<TransportLineVO>> queryAll() {
        R<List<TransportLineVO>> r = transportLineService.queryAll();
        return r;
    }

    /**
     * 根据车次Id查询出车辆,司机
     *
     * @param id 车次Id
     * @return
     */
    @ApiOperation("根据车次Id查询出车辆,司机")
    @ApiImplicitParam(name = "id", value = "车次Id")
    @GetMapping("/findTransportTripsById/{id}")
    public R<List<TransportTripsDto>> findTransportTripsById(@PathVariable String id) {
        R<List<TransportTripsDto>> dto = transportLineService.findTransportTripsById(id);
        return dto;
    }
}

